import { useEffect } from "react";
import { useReactive } from "ahooks";
import { useModel } from "umi";
import { message } from "antd";

export default function gatewayInfoModal() {
    const { remoteData } = useModel("global");
    const { baseInfo } = useModel("person.info");

    const state = useReactive({
        gatewayLoading: false,
        organizationLoading: false,
        gateways: [],
        selectedGateway: {},
        organizations: [],
        selectedPerson: {},
        organizationServices: [],
    });

    const setCurrentGateway = (gateway) => {
        state.selectedGateway = gateway;
    };

    const setCurrentPerson = (person) => {
        state.selectedPerson = person || {};
    };

    const resetInit = async () => {
        if (state.selectedPerson.uuid) {
            let res = await remoteData({
                object: "multi-organization-gateway",
                serviceUrl: state.selectedGateway.url,
                serviceName: "/api/v2/organization/resetInitialization",
                organizationUuid: state.selectedPerson.uuid,
                params: { uuid: state.selectedPerson.uuid },
            });
            if (res) {
                updateSelectedPerson({ initializationStatus: false, initializationTime: null });
            }
        }
    };

    return {
        ...state,
        setCurrentGateway,
        setCurrentPerson,
        resetInit,
    };
}
